<?php
/**
 * 自定义异常类
 *
 */

abstract class MyException extends Exception
{
    var $tpl;

    function __construct($message)
    {
        global $tpl,$sys_conf_var;

        if($sys_conf_var['system']['error'] === false) die();

        $args = func_get_args();

        parent::__construct($message,$args[1]);

        if(!is_object($tpl))
        {
            $tpl = new Compiler();
            $tpl->compile_dir  = CACHE_DIR  . 'compile/';
        }

        $this->tpl = $tpl;
        $this->tpl->template_dir = SYS_PATH  . 'template/error/';
    }

    abstract function showError();

    function __destruct()
    {
        exit;
    }
}

class MySqlException extends MyException
{
    /**
     * 得到异常错误信息
     *
     * @return string
     */
    function getError()
    {
        //error message
        $errorMsg = 'Error number : <b>' . mysql_errno() . '</b><br>Error message : <b>' . mysql_error() . '</b>';
        return $errorMsg;
    }

    /**
     * 得到异常错误请求语句
     *
     * @return string
     */
    function getQuery()
    {
        //error message
        if($this->getMessage())
        {
            $errorMsg =  'Error query : <b>' . $this->getMessage() . '</b>';
            return $errorMsg;
        }
    }

    /**
     * 显示错误
     *
     */
    function showError()
    {
        $this->tpl->assign('Error',$this->getError());
        $this->tpl->assign('Query',$this->getQuery());

        $this->tpl->display('error_db.html');
    }
}

class AppException extends MyException
{
    /**
     * 显示错误
     *
     */
    function showError()
    {
        $this->tpl->assign('Error','Error : <b>' . $this->getMessage() . '</b>');
        $this->tpl->assign('Trace',$this->getTraceAsString());

        $this->tpl->display('error_app.html');
    }
}
?>
